import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:ost_flutter_module/class/store/course_detail_page/bloc/course_detail_page_bloc.dart';
import 'package:ost_flutter_module/common/color.dart';
import 'package:ost_flutter_module/common/styles.dart';
import 'package:ost_flutter_module/widget/custom_widget/custome_scaffold.dart';
import 'package:ost_flutter_module/widget/custom_widget/custome_share.dart';
import 'package:ost_flutter_module/widget/custom_widget/text_button.dart';
import 'package:ost_flutter_module/widget/dialog/show_bottom_sheet.dart';
import 'package:ost_flutter_module/widget/loading/loading_empty_widget.dart';
import 'package:ost_flutter_module/widget/loading/loading_error_widget.dart';
import 'package:ost_flutter_module/widget/loading/loading_view.dart';
import 'package:ost_flutter_module/widget/loading/loading_widget.dart';

import 'store_course_detail_page_body.dart';
import 'store_course_detail_page_bottom_navigation_bar.dart';

class StoreCourseDetailPage extends StatelessWidget {
  final String id;

  const StoreCourseDetailPage({Key key, this.id}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BlocProvider<CourseDetailPageBloc>(
      create: (context) =>
          CourseDetailPageBloc()..add(CourseDetailPageFetchEvent(id: id)),
      child: StoreCourseDetailPageLoading(id: id),
    );
  }
}

class StoreCourseDetailPageLoading extends StatelessWidget {
  final String id;

  const StoreCourseDetailPageLoading({Key key, this.id}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<CourseDetailPageBloc, CourseDetailPageState>(
      builder: (context, state) {
        var _dataModel = context.read<CourseDetailPageBloc>().dataModel;

        return GScaffold(
          title: Text('课程详情', style: TextStyles.appBarTitleStyle),
          actions: [
            GTextButton(
              title: '分享',
              normalForgroundColor: color303030,
              onPressed: () {
                gShowCancelBottomSheet(
                  context,
                  child: GShare(),
                  cancel: () {},
                );
              },
            )
          ],
          body: LoadingView(
            status: _dataModel?.loadingStatus,
            loadingContent: LoadingWidget(),
            errorContent: LoadingErrorWidget(
              errorTitle: _dataModel?.errorMsg,
              onRetry: () => context
                  .read<CourseDetailPageBloc>()
                  .add(CourseDetailPageFetchEvent(id: id)),
            ),
            successContent:
                StoreCourseDetailPageBody(entity: _dataModel.entity),
            emptyContent: LoadingEmptyWidget(),
          ),
          bottomNavigationBar: StoreCourseDetailPageBottomNavigationBar(),
        );
      },
    );
  }
}
